
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;
 
using JYJ.AiYard.StationManagement.TbDigitalPaychk.Dtos;
using JYJ.AiYard.StationManagement.TbDigitalPaychk.DomainService;
using JYJ.AiYard.StationManagement.TbDigitalPaychk.Authorization;
using JYJ.AiYard.StationManagement.YardReport;
using JYJ.AiYard.StationManagement.YardCommonHald;

namespace JYJ.AiYard.StationManagement.TbDigitalPaychk
{
    /// <summary>
    /// TbDigitalPaychk应用层服务的接口实现方法  
    ///</summary>
    [AbpAuthorize]
    public class TbDigitalPaychkAppService : AiYardAppServiceBase, ITbDigitalPaychkAppService
    {
        private readonly IRepository<TbDigitalPaychk, long> _entityRepository;

        private readonly ITbDigitalPaychkManager _entityManager;

        /// <summary>
        /// 构造函数 
        ///</summary>
        public TbDigitalPaychkAppService(
        IRepository<TbDigitalPaychk, long> entityRepository
        ,ITbDigitalPaychkManager entityManager
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
        }


        /// <summary>
        /// 获取TbDigitalPaychk的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>
		[AbpAuthorize(TbDigitalPaychkPermissions.Query)] 
        public async Task<PagedResultDto<TbDigitalPaychkListDto>> GetPaged(GetTbDigitalPaychksInput input)
		{
			List<long> StationList = new List<long>();
			tbQFilterText filterText = input.FilterText.ToObject<tbQFilterText>();
			if (filterText.StationId == null || filterText.StationId <= 0)
			{
				StationList = GetCurrenUserOrganizationAndChildrenUnit();
			}
			else
			{
				StationList.Add((long)filterText.StationId);
			}
			var query = _entityRepository.GetAll().Where(o => StationList.Contains(o.Station_ID));
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query               
                    .OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<TbDigitalPaychkListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<TbDigitalPaychkListDto>>();

			return new PagedResultDto<TbDigitalPaychkListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取TbDigitalPaychkListDto信息
		/// </summary>
		[AbpAuthorize(TbDigitalPaychkPermissions.Query)] 
		public async Task<TbDigitalPaychkListDto> GetById(EntityDto<long> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<TbDigitalPaychkListDto>();
		}

		/// <summary>
		/// 获取编辑 TbDigitalPaychk
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbDigitalPaychkPermissions.Create,TbDigitalPaychkPermissions.Edit)]
		public async Task<GetTbDigitalPaychkForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetTbDigitalPaychkForEditOutput();
            TbDigitalPaychkEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<TbDigitalPaychkEditDto>(); 
			}
			else
			{
				editDto = new TbDigitalPaychkEditDto();
			}

			output.TbDigitalPaychk = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改TbDigitalPaychk的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbDigitalPaychkPermissions.Create,TbDigitalPaychkPermissions.Edit)]
		public async Task CreateOrUpdate(CreateOrUpdateTbDigitalPaychkInput input)
		{
            if (input.TbDigitalPaychk.Station_ID <= 0)
            {
                throw new UserFriendlyException("请选择确定的场所名称");
            }
			if (input.TbDigitalPaychk.aud_0 == input.TbDigitalPaychk.aud_1) {
				throw new UserFriendlyException("申请和审核人不允许是同一个");
			}
            if (input.TbDigitalPaychk.Id.HasValue)
			{
				await Update(input.TbDigitalPaychk);
			}
			else
			{
				await Create(input.TbDigitalPaychk);
			}
		}


		/// <summary>
		/// 新增TbDigitalPaychk
		/// </summary>
		[AbpAuthorize(TbDigitalPaychkPermissions.Create)]
		protected virtual async Task<TbDigitalPaychkEditDto> Create(TbDigitalPaychkEditDto input)
		{
             
            //判断相同的客户端是否只存在一个
            var query = _entityRepository.GetAll()
                .Where( o=>o.Station_ID == input.Station_ID);
            if (query.Count() > 0)
            {
                throw new UserFriendlyException("存在相同场所参数设置");
            } 
            var entity=input.MapTo<TbDigitalPaychk>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<TbDigitalPaychkEditDto>();
		}

		/// <summary>
		/// 编辑TbDigitalPaychk
		/// </summary>
		[AbpAuthorize(TbDigitalPaychkPermissions.Edit)]
		protected virtual async Task Update(TbDigitalPaychkEditDto input)
		{  
            //判断相同的客户端是否只存在一个
            var query = _entityRepository.GetAll()
                .Where(o => o.Station_ID == input.Station_ID && o.Id!=input.Id);
            if (query.Count() > 0)
            {
                throw new UserFriendlyException("存在相同场所参数设置");
            }

            var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity); 
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除TbDigitalPaychk信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[AbpAuthorize(TbDigitalPaychkPermissions.Delete)]
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除TbDigitalPaychk的方法
		/// </summary>
		[AbpAuthorize(TbDigitalPaychkPermissions.BatchDelete)]
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		///// <summary>
		///// 导出TbDigitalPaychk为excel表,等待开发。
		///// </summary>
		///// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


